package com.jinlejia.erp.ui.mine.contract;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.BaseActivity;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityMyContractDetailsBinding;
import com.jinlejia.erp.ui.common.CallUpDialog;
import com.jinlejia.erp.ui.customer.DetailCustomerActivity;
import com.jinlejia.erp.ui.house.HouseDetailActivity;
import com.jinlejia.erp.ui.mine.process.ProcessProgressActivity;
import com.jinlejia.erp.utils.CallUtils;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.Map;

import okhttp3.Call;

/**
 * 我的合同详情(activity)
 */

public class MyContractDetailsActivity extends BaseActivity {
    private CallUpDialog dialog;
    private String Customer_phone_number, owner_phone_number, stepName, rscode, cscode, chose;
    private int state = -1;


    private ActivityMyContractDetailsBinding binding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_my_contract_details);
        binding.setEvent(this);
        initView();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        getData();
        //客源编号
        binding.contactDetailsPassengerNumber.setOnClickListener(view -> {
            Intent intent = new Intent(MyContractDetailsActivity.this, DetailCustomerActivity.class);
            intent.putExtra("csCode", cscode);
            intent.putExtra("type", chose);
            startActivity(intent);
        });
        //房源编号
        binding.contactDetailsHouseNumber.setOnClickListener(view -> {
            Intent intent = new Intent(MyContractDetailsActivity.this, HouseDetailActivity.class);
            intent.putExtra("code", rscode);
            intent.putExtra("type", chose);
            startActivity(intent);
        });
    }

    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.back_btn:                  //返回
                finish();
                break;
            case R.id.contact_details_link1:      //客户电话
                dialog = new CallUpDialog(MyContractDetailsActivity.this, Customer_phone_number);
                dialog.setConfirmListener(() -> callTask(Customer_phone_number));
                dialog.show();
                break;
            case R.id.contact_details_link2:      //业主电话
                dialog = new CallUpDialog(MyContractDetailsActivity.this, owner_phone_number);
                dialog.setConfirmListener(() -> callTask(owner_phone_number));
                dialog.show();
                break;
            case R.id.details_schedule:          //办件进度
                intent = new Intent(MyContractDetailsActivity.this, ProcessProgressActivity.class);
                intent.putExtra("contractCode", getIntent().getStringExtra("contractCode"));
                startActivity(intent);
                break;
            case R.id.divide_into:               //分成
                intent = new Intent(MyContractDetailsActivity.this, ContractIntoActivity.class);
                intent.putExtra("contractCode", getIntent().getStringExtra("contractCode"));
                startActivity(intent);
                break;
            case R.id.sell_progress: //销售进度
                intent = new Intent(MyContractDetailsActivity.this, SellProgressActivity.class);
                intent.putExtra("contractCode", getIntent().getStringExtra("contractCode"));
                startActivity(intent);
                break;
            case R.id.tvContractProcess://合同进程跟踪
                intent = new Intent(MyContractDetailsActivity.this, ContractProgressTrackingActivity.class);
                intent.putExtra("pactCode",getIntent().getStringExtra("contractCode"));
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    /**
     * 获取数据
     */
    private void getData() {
        OkHttpUtils
                .get()
                .url(ApiConfig.CONTRACT_DETAILS)
                .addParams("contractCode", getIntent().getStringExtra("contractCode"))
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(MyContractDetailsActivity.this, response, null)) {
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            binding.contactDetailsNumber.setText(data.getString("contractCode"));
                            binding.contactDetailsPassengerNumber.setText(data.getString("csCode"));
                            if (!TextUtils.isEmpty(data.getString("csCode")))
                                cscode = data.get("csCode").toString();
                            binding.detailsPassengerName.setText(data.getString("customerName"));
                            binding.contactDetailsHouseNumber.setText(data.getString("rscode"));
                            if (!TextUtils.isEmpty(data.getString("rscode")))
                                rscode = data.get("rscode").toString();
                            binding.detailsHouseName.setText(data.getString("ownerName"));
                            binding.contactDetailsPeople.setText(data.getString("tradeUserName"));
                            binding.contactDetailsAddress.setText(data.getString("tradeyDeptName"));
                            binding.contactDetailsPropertyAddress.setText(data.getString("address"));
                            switch (data.getString("contractType")) {
                                case "0":
                                    chose = "sale";
                                    binding.contactDetailsState.setText("买卖");
                                    binding.contactDetailsMoney.setText(data.getString("tradePrice").concat("万"));
                                    binding.tvContractProcess.setVisibility(View.GONE);
                                    break;
                                case "1":
                                    chose = "rent";
                                    binding.contactDetailsMoney.setText(data.getString("rentPrice").concat("元"));
                                    binding.contactDetailsState.setText("租赁");
                                    binding.tvContractProcess.setVisibility(View.GONE);
                                    break;
                                case "2":
                                    chose = "sale";
                                    binding.contactDetailsState.setText("一手房");
                                    binding.contactDetailsMoney.setText(data.getString("tradePrice").concat("万"));
                                    binding.tvContractProcess.setVisibility(View.VISIBLE);
                                    break;
                                case "3":
                                    chose = "sale";
                                    binding.contactDetailsState.setText("垫资");
                                    binding.contactDetailsMoney.setText(data.getString("tradePrice").concat("万"));
                                    binding.tvContractProcess.setVisibility(View.GONE);
                                    break;
                                default:
                                    break;
                            }
                            if (!TextUtils.isEmpty(data.getString("customerTel"))) {
                                Customer_phone_number = data.get("customerTel").toString();
                                state = 0;
                            }
                            if (!TextUtils.isEmpty(data.getString("ownerTel"))) {
                                owner_phone_number = data.get("ownerTel").toString();
                                state = 1;
                            }
                            if (data.getString("transferState").equals("1")) {
                                if (!TextUtils.isEmpty(data.getString("stepName"))) {
                                    binding.type.setText(data.getString("stepName"));
                                    stepName = data.getString("stepName");
                                }
                                binding.detailsSchedule.setVisibility(View.VISIBLE);
                            }

                            switch (data.getIntValue("commissionState")) {
                                case 0:
                                    binding.commissionState.setText("未结佣");
                                    break;
                                case 1:
                                    binding.commissionState.setText("收佣结佣");
                                    break;
                                case 2:
                                    binding.commissionState.setText("过户结佣");
                                    break;
                                case 3:
                                    binding.commissionState.setText("结件结佣");
                                    break;
                                case 4:
                                    binding.commissionState.setText("批复结佣");
                                    break;
                                case 5:
                                    binding.commissionState.setText("申请结佣");
                                    break;
                                case 6:
                                    binding.commissionState.setText("已解约");
                                    break;
                                default:
                                    break;
                            }
                            if (data.containsKey("customerSource") && data.getString("customerSource").endsWith("房微爱")) {
                                binding.sellProgress.setVisibility(View.VISIBLE);
                            } else {
                                binding.sellProgress.setVisibility(View.GONE);
                            }
                        }
                    }
                });
    }

    public void callTask(String phone) {
        CallUtils.CallTel(MyContractDetailsActivity.this, phone);
    }

}
